package com.ojoin.ojoin.eb_spider.service.impl;
 
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.jasypt.commons.CommonUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.utils.Config;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformInfo;;

@Service("fireFox") 
public class FireFoxWebDriverServiceimpl extends BasicWebDriverServiceImpl {

	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;
	private static Logger log = LoggerFactory
			.getLogger(FireFoxWebDriverServiceimpl.class);

	public void initWebDriver() throws Exception {
		initWebDriver(null);
	}

	@Override
	public void initWebDriver(String downloadFilepath) throws Exception {
		WebDriver driver = null;
		try{
			String firefoxBinPath = Config.getProperty(TbSlPlatformInfo.sl_config,
					"webdriver.firefox.bin");

			LogUtils.info(log, "webdriver.firefox.bin= %s", firefoxBinPath);

			File firefox = new File(firefoxBinPath);

			System.setProperty("webdriver.firefox.bin", firefox.getAbsolutePath());
			ProfilesIni allProfiles = new ProfilesIni();
			FirefoxProfile profile = allProfiles.getProfile("default");
			profile.setPreference("plugin.default.state", 2);
			// 下载
			profile.setPreference("browser.download.folderList", 2);
			profile.setPreference("browser.helperApps.alwaysAsk.force", false);
			profile.setPreference("browser.download.manager.showWhenStarting",
					false);

			profile.setPreference("browser.download.dir", downloadFilepath);
			profile.setPreference(
					"browser.helperApps.neverAsk.saveToDisk",
					"application/vnd.ms-excel,application/octet-stream,text/plain,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

			driver = new FirefoxDriver(profile);
		}catch(Exception e){
			LogUtils.info(log, e.toString());
		}
		
		this.setDriver(driver);

	}

	@Override
	public void initRemoteWebDriver(String downloadFilepath) throws Exception {
		WebDriver driver = null;
		try {

			FirefoxProfile profile = new FirefoxProfile();

			// 总是授权
			profile.setPreference("plugin.default.state", 2);
			// 下载
			profile.setPreference("browser.download.folderList", 2);
			profile.setPreference("browser.helperApps.alwaysAsk.force", false);
			profile.setPreference("browser.download.manager.showWhenStarting",
					false);
			if (!CommonUtils.isEmpty(downloadFilepath)) {
				profile.setPreference("browser.download.dir", downloadFilepath);
			}

			profile.setPreference(
					"browser.helperApps.neverAsk.saveToDisk",
					"application/vnd.ms-excel,application/octet-stream,text/plain,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

			DesiredCapabilities capabilities = init(downloadFilepath);

			String gridHub = "http://"
					+ Config.getProperty(TbSlPlatformInfo.sl_config,
							"webdriver.remote.host") + ":8880/wd/hub";
			driver = new RemoteWebDriver(new URL(gridHub), capabilities);

			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			driver.manage().window().maximize();
		} catch (Exception e) {
			LogUtils.info(log, "创建WebDriver出错", e);
			throw e;
		} finally {

		}
		this.setDriver(driver);

	}

	public DesiredCapabilities init(String downloadFilepath) {

		FirefoxProfile profile = new FirefoxProfile();

		// 总是授权
		profile.setPreference("plugin.default.state", 2);
		// 下载
		profile.setPreference("browser.download.folderList", 2);
		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
		profile.setPreference("browser.download.manager.showWhenStarting",
				false);
		if (!CommonUtils.isEmpty(downloadFilepath)) {
			profile.setPreference("browser.download.dir", downloadFilepath);
		}

		profile.setPreference(
				"browser.helperApps.neverAsk.saveToDisk",
				"application/vnd.ms-excel,application/octet-stream,text/plain,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability(FirefoxDriver.PROFILE, profile);

		return capabilities;
	}

}
